package com.share.schedule.quartz.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.share.common.common.Page;
import com.share.common.common.Result;
import com.share.common.utils.StrUtil;
import com.share.schedule.quartz.constant.Constants;
import com.share.schedule.quartz.domain.SysJob;
import com.share.schedule.quartz.service.ISysJobService;
import com.share.schedule.quartz.util.CronUtils;
import com.share.schedule.quartz.util.ScheduleUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 调度任务信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/job")
public class SysJobController {
    @Autowired
    private ISysJobService jobService;

    /**
     * 查询定时任务列表
     */
    @GetMapping("/list")
    public Page<SysJob> list(SysJob req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<SysJob> list = jobService.selectJobList(req);
        return new Page<>(list, new PageInfo<>(list).getTotal(), req);
    }


    /**
     * 获取定时任务详细信息
     */
    @GetMapping(value = "/{jobId}")
    public Result getInfo(@PathVariable("jobId") Long jobId) {
        return Result.success(jobService.selectJobById(jobId));
    }

    /**
     * 新增定时任务
     */
    @PostMapping
    public Result add(@RequestBody SysJob job) throws Exception {
        if (!CronUtils.isValid(job.getCronExpression())) {
            return Result.failure("新增任务'" + job.getJobName() + "'失败，Cron表达式不正确");
        } else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI)) {
            return Result.failure("新增任务'" + job.getJobName() + "'失败，目标字符串不允许'rmi'调用");
        } else if (StrUtil.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.LOOKUP_LDAP, Constants.LOOKUP_LDAPS})) {
            return Result.failure("新增任务'" + job.getJobName() + "'失败，目标字符串不允许'ldap(s)'调用");
        } else if (StrUtil.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.HTTP, Constants.HTTPS})) {
            return Result.failure("新增任务'" + job.getJobName() + "'失败，目标字符串不允许'http(s)'调用");
        } else if (StrUtil.containsAnyIgnoreCase(job.getInvokeTarget(), Constants.JOB_ERROR_STR)) {
            return Result.failure("新增任务'" + job.getJobName() + "'失败，目标字符串存在违规");
        } else if (!ScheduleUtils.whiteList(job.getInvokeTarget())) {
            return Result.failure("新增任务'" + job.getJobName() + "'失败，目标字符串不在白名单内");
        }
        return Result.success(jobService.insertJob(job));
    }

    /**
     * 修改定时任务
     */
    @PutMapping
    public Result edit(@RequestBody SysJob job) throws Exception {
        if (!CronUtils.isValid(job.getCronExpression())) {
            return Result.failure("修改任务'" + job.getJobName() + "'失败，Cron表达式不正确");
        } else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI)) {
            return Result.failure("修改任务'" + job.getJobName() + "'失败，目标字符串不允许'rmi'调用");
        } else if (StrUtil.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.LOOKUP_LDAP, Constants.LOOKUP_LDAPS})) {
            return Result.failure("修改任务'" + job.getJobName() + "'失败，目标字符串不允许'ldap(s)'调用");
        } else if (StrUtil.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.HTTP, Constants.HTTPS})) {
            return Result.failure("修改任务'" + job.getJobName() + "'失败，目标字符串不允许'http(s)'调用");
        } else if (StrUtil.containsAnyIgnoreCase(job.getInvokeTarget(), Constants.JOB_ERROR_STR)) {
            return Result.failure("修改任务'" + job.getJobName() + "'失败，目标字符串存在违规");
        } else if (!ScheduleUtils.whiteList(job.getInvokeTarget())) {
            return Result.failure("修改任务'" + job.getJobName() + "'失败，目标字符串不在白名单内");
        }
        return Result.insertOrDeleteOrUpdate(jobService.updateJob(job));
    }

    /**
     * 定时任务状态修改
     */
    @PutMapping("/changeStatus")
    public Result changeStatus(@RequestBody SysJob job) throws Exception {
        SysJob newJob = jobService.selectJobById(job.getJobId());
        newJob.setStatus(job.getStatus());
        return Result.insertOrDeleteOrUpdate(jobService.changeStatus(newJob));
    }

    /**
     * 定时任务立即执行一次
     */
    @PutMapping("/run")
    public Result run(@RequestBody SysJob job) throws Exception {
        boolean result = jobService.run(job);
        return Result.bool(result, "定时任务已过期");
    }

    /**
     * 删除定时任务
     */
    @DeleteMapping("/{jobIds}")
    public Result remove(@PathVariable Long[] jobIds) throws Exception {
        jobService.deleteJobByIds(jobIds);
        return Result.success();
    }
}
